package cn.vc.content.controller.article;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.content.entity.article.Article;
import cn.vc.content.service.article.ArticleHistoryService;
import cn.vc.content.service.article.ArticleService;
import cn.vc.content.util.SecurityUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleHistoryService articleHistoryService;



    /**
     * 发布文章
     * @param title 文章标题
     * @param intro 文章简介
     * @param content 文章内容
     * @return
     */
    @PostMapping("/publish")
    public Result<?> publish(@RequestParam String title, @RequestParam String intro, /*@RequestParam String communitiesId,*/
                             @RequestParam String coverUrl,
                             @RequestBody String content) throws IOException {
//        VUser myUser = SecurityUtil.getUser();
//        if (myUser==null) return Result.NOT_LOGGED_IN();
//        // 是否保存成功
//        boolean bo = true;
//        UUID id = UUID.randomUUID();//文件名
//        Article article = new Article();
//        article.setTitle(title);
//        article.setIntro(intro);
//        article.setContent(content);
//        article.setCoverUrl(coverUrl);
////        video.setCommunitiesId(communitiesId);
//        article.setUserId(myUser.getId());
//        articleService.add(article);
//        return new Result<>(bo);

        System.out.println(content);
        return new Result<>(1);
    }
    /**
     * 审核通过
     */
    @PostMapping("/audit/{id}")
    public void passTheAudit(@PathVariable("id") Long id) {
        articleService.passTheAudit(id);
    }



    /**
     * <h2>查询所有草稿</h2>
     * @return
     */
    @GetMapping("/1")
    public List<Article> selectDraft() {
        List<Article> articleList = articleService.selectAllByIsDraft();
        return articleList;
    }
    /**
     * <h2>通过ID查询草稿</h2>
     * @return
     */
    @GetMapping("/1/{id}")
    public Result<?> selectDraftById(@PathVariable("id") String id) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        String json = stringRedisTemplate.opsForValue().get("article:"+myUser.getId()+":"+id);
        return new Result<>(1, json);
    }
    /**
     * <h2>查询所有文章</h2>
     * @return
     */
    @GetMapping("/2")
    public Result<List<Article>> selectArticleAll() {
        List<Article> articleList = articleService.selectAllByNotDraft();
        return new Result<>(1, articleList);
    }
    /**
     * 通过ID查询文章
     * @return
     */
    @GetMapping("/2/{id}")
    public Result<?> selectSaveById(@PathVariable("id") Long id) {
        Article article = articleService.selectById(id);
        // 文章不存在
        if (article==null) return new Result<>(0);
        VUser myUser = SecurityUtil.getUser();
        // 判断是不是登录用户的请求
        if (myUser!=null) {
            // 确定为登录用户后记录文章历史记录
            // 添加观看文章记录
            articleHistoryService.records(myUser.getId(), id);
            // 添加点击数
            articleService.tap(id);
        }
        return new Result<>(1, article);
    }
}
